package com.coocaa.app.gamecenter.pages.arsenal.view;

import android.content.Context;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.coocaa.app.gamecenter.pages.arsenal.data.WeaponItemData;
import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.uipackage.utils.CoocaaFresco;
import com.coocaa.x.uipackage.widget.CoocaaDraweeView;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.tianci.appstore.R;
import com.tianci.appstore.utils.AsyncTask;

/**
 * Created by xfk on 2015/11/7.
 */
public class ItemView extends RelativeLayout {
    private static String tag = "arsenal";
    private Context mContext;
    private FrameLayout imageLayout;
    private CoocaaDraweeView iconView;
    private TextView textView;
    private WeaponItemData weaponItemData;
    private boolean isRecycleCache = false;
    public ItemView(Context context) {
        super(context);
        mContext = context;
        createView();
    }

    private void createView() {

//        this.setBackgroundResource(R.drawable.game_center_arsenal_item_selector);
        this.setBackgroundResource(R.drawable.arsenal_item_bg);
        imageLayout = new FrameLayout(mContext);
        LayoutParams params = new LayoutParams(CoocaaApplication.Div(235), CoocaaApplication.Div(273));
        params.topMargin = CoocaaApplication.Div(8);
        params.leftMargin = CoocaaApplication.Div(21);
        this.addView(imageLayout, params);
        imageLayout.setFocusable(false);

        textView = new TextView(mContext);
        textView.setText("");
        textView.setTextSize(CoocaaApplication.Dpi(28));
        textView.setFocusable(false);
        textView.setClickable(false);
        textView.setGravity(Gravity.CENTER);
        LayoutParams txtParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        txtParams.topMargin = CoocaaApplication.Div(222);
        this.addView(textView, txtParams);
        this.setFocusable(true);
    }

    public void setData(WeaponItemData itemData) {
        if(itemData == null)
            return;
        this.weaponItemData = itemData;

        AsyncTask.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (weaponItemData == null)
                    return;
                if(weaponItemData.price != null && !weaponItemData.price.equals(""))
                    textView.setText("￥"+weaponItemData.price);
                else
                    textView.setText("");
                if (weaponItemData.smallUrl == null)
                    return;
                iconView = setImageUrl(weaponItemData.smallUrl);
                if (iconView != null) {
                    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.MATCH_PARENT);
                    imageLayout.addView(iconView, params);
                }
            }
        });
    }

    public WeaponItemData getWeaponItemData() {
        return this.weaponItemData;
    }

    private CoocaaDraweeView setImageUrl(String url) {

        try {
            String path = url;

            CoocaaDraweeView mSimpleDraweeView = new CoocaaDraweeView(mContext);
            mSimpleDraweeView
                    .getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.CENTER_INSIDE);
//            mSimpleDraweeView
//                    .getHierarchy().setPlaceholderImage(R.drawable.bg);
            Uri uri = Uri.parse(path);
            int width = CoocaaApplication.Div(235), height = CoocaaApplication.Div(273);
            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                    .setResizeOptions(new ResizeOptions(width, height))
                    .build();
            DraweeController controller = CoocaaFresco.newDraweeControllerBuilder(mContext)
                    .setControllerListener(controllerListener)
                    .setOldController(mSimpleDraweeView.getController())
                    .setImageRequest(request)
                    .build();
            mSimpleDraweeView.setController(controller);

            RoundingParams roundingParams = RoundingParams.fromCornersRadius(10f);
            mSimpleDraweeView.getHierarchy().setRoundingParams(roundingParams);

            return mSimpleDraweeView;
        } catch (NullPointerException e) {
            LogUtils.i(tag, "" + e);
        }

        return null;
    }

    ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
        @Override
        public void onSubmit(String s, Object o) {
            LogUtils.i(tag, "sunny facebook onSubmit s:" + s);
        }

        @Override
        public void onFinalImageSet(String s, ImageInfo o, Animatable animatable) {
            LogUtils.i("facebook", "sunny facebook onFinalImageSet s:" + s + ";   o:" + o.toString());
        }

        @Override
        public void onIntermediateImageFailed(String s, Throwable throwable) {
            LogUtils.i("facebook", "sunny facebook onIntermediateImageFailed s:" + s + ";   throwable:" + throwable.toString());
        }

        @Override
        public void onFailure(String s, Throwable throwable) {
            LogUtils.i("facebook", "sunny facebook onFailure s:" + s + ";   throwable:" + throwable.toString());
            if (weaponItemData != null
                    && weaponItemData.smallUrl != null
                    && iconView != null)
                iconView.setImageURI(Uri.parse(weaponItemData.smallUrl));
        }

        @Override
        public void onRelease(String s) {
            LogUtils.i("facebook", "sunny facebook onRelease s:" + s);
        }
    };

    public void recycleItemCache(boolean b) {
        if(b == isRecycleCache)
            return;

        LogUtils.i(tag, "recycleItemCache: "+b+", isRecycleCache: "+ isRecycleCache);
        if(b) {
            if(iconView != null) {
                try {
                    iconView.setDrawingCacheEnabled(true);
                    if(iconView.getDrawingCache() != null)
                        iconView.getDrawingCache().recycle();
                } catch (RuntimeException e) {
                    iconView.destroyDrawingCache();
                    LogUtils.i(tag, e.toString());
                }
            }

            if(textView != null) {
                textView.destroyDrawingCache();
            }
        } else {
            if(weaponItemData == null)
                return;

            if(weaponItemData != null && iconView != null) {
                iconView.setImageURI(Uri.parse(weaponItemData.smallUrl));
            }

            if(weaponItemData.price != null && !weaponItemData.price.equals(""))
                textView.setText("￥"+weaponItemData.price);
            else
                textView.setText("");
        }

        isRecycleCache = b;
    }

    public void hideItemView() {
        if(iconView != null) {
            try {
                iconView.setDrawingCacheEnabled(true);
                if(iconView.getDrawingCache() != null)
                    iconView.getDrawingCache().recycle();
            } catch (RuntimeException e) {
                iconView.destroyDrawingCache();
                LogUtils.i(tag, e.toString());
            }
        }

        if(textView != null) {
            textView.destroyDrawingCache();
        }
    }

    public void showItemView() {

    }
    public void onDestroy() {
        if(imageLayout != null) {
            imageLayout.destroyDrawingCache();
            imageLayout.removeAllViews();
            imageLayout = null;
        }

        if(iconView != null) {
            try {
                iconView.setDrawingCacheEnabled(true);
                if(iconView.getDrawingCache() != null)
                    iconView.getDrawingCache().recycle();
            } catch (RuntimeException e) {
                LogUtils.i(tag, e.toString());
            } finally {
                iconView.destroyDrawingCache();
                iconView = null;
            }
        }

        if(textView != null) {
            textView.destroyDrawingCache();
            textView = null;
        }
    }
}
